*This file takes the original CCES data files and prepares them for analysis.

*There are 4 parts to this file. Running the entire do file will transform the original raw CCES files into the final data files used for analysis.

*Part I. Renaming variables, generating year variable, generating age variable

clear all
use c:\users\meyera\Dropbox\research\climatechange_politics\data_documentation_jaere\original_cces_data_jaere\cces_2006

rename (	v2092	v3023	v2004	v2005		v3005	v3007		v2018	v2020 v1002	v1001 v2021) (climatechange	environment_list	gender	race		partyid3	partyid7		education	birthyear	stateid	weight ideology)
keep climatechange environment_list	gender	race		partyid3	partyid7		education	birthyear	stateid	weight ideology
gen year_match=2006
gen age=year_match-birthyear
save cces_06_formerge, replace

clear all
use cces_2007.dta

rename (CC06_V2092		CC06_V3022			CC06_V3005	CC06_V3007		CC06_V2018	CC06_V2020		newsint	CC06_V2042	CC06_V1002	weight ideo5) (climatechange		environment_list		partyid3	partyid7		education	birthyear	news	politics	stateid	weight ideology)
keep climatechange		environment_list	gender	race		partyid3	partyid7		education	birthyear	news	politics	stateid	weight ideology
gen year_match=2007
gen age=year_match-birthyear
save cces_07_formerge, replace

clear all
use cces_2008.dta

rename (		CC311	V208	V211		CC307	CC307a		V213	V207	V251	V244	V245		V201 V243) (		environment_list	gender	race		partyid3	partyid7		education	birthyear	statefips	news	politics	weight ideology)
keep 		environment_list	gender	race		partyid3	partyid7		education	birthyear	statefips	news	politics	weight ideology
gen year_match=2008
gen age=year_match-birthyear
destring statefips, replace
save cces_08_formerge, replace


clear all
use cces_2009.dta

rename (cc09_51		v208	v211		cc423	cc424		v213	v207	v265	v244 v200 v261) (climatechange		gender	race		partyid3	partyid7		education	birthyear	statefips	news weight ideology)
keep climatechange		gender	race		partyid3	partyid7		education	birthyear	statefips	news weight ideology
gen year_match=2009
gen age=year_match-birthyear
destring statefips, replace
save cces_09_formerge, replace

clear all
use cces_2010.dta
rename (CC321		CC325	V208	V211	CC350	V212a	V212d		V213	V207	V302	V244 V101 V243 CC317) (climatechange		environment_list	gender	race	partyreg	partyid3	partyid7	education	birthyear	statefips	news weight ideology elec_2008)
keep climatechange		environment_list	gender	race	partyreg	partyid3	partyid7	education	birthyear	statefips	news weight ideology elec_2008
gen year_match=2010
gen age=year_match-birthyear
destring statefips, replace
save cces_10_formerge, replace

clear all
use cces_2011.dta
rename (CC350		V208	V211	CC308	V212a	V212d		V213	V207	V302	V244 V101 V243) (climatechange		gender	race	partyreg	partyid3	partyid7	education	birthyear	statefips	news weight ideology)
keep climatechange		gender	race	partyreg	partyid3	partyid7	education	birthyear	statefips	news weight ideology
gen year_match=2011
gen age=year_match-birthyear
destring statefips, replace
save cces_11_formerge, replace

clear all
use cces_2012.dta
rename (CC321			CC325	gender	race	CC350	pid3	pid7	CC421a	educ	birthyr	inputstate	newsint V103 ideo5 CC317)(climatechange		environment_list	gender	race	partyreg	partyid3	partyid7	pid3_post	education	birthyear	statefips	news weight ideology elec_2008)
keep climatechange		environment_list	gender	race	partyreg	partyid3	partyid7	pid3_post	education	birthyear	statefips	news weight ideology elec_2008
gen year_match=2012
gen age=year_match-birthyear
save cces_12_formerge, replace

clear all
use cces_panel_download.dta

rename (CC10_321		CC10_325	gender_10	race_10		pid3_10	pid7_10	educ_10	birthyr_10	inputstate_10	newsint_10		ideo5_10)(climatechange2010		environment2010	gender2010	race2010		partyid32010	partyid72010	education2010	birthyear2010	statefips2010	news2010	ideology2010)
rename (CC12_321		CC12_325	gender_12	race_12		pid3_12	pid7_12	educ_12	birthyr_12	inputstate_12	newsint_12		ideo5_12)(climatechange2012		environment2012	gender2012	race2012		partyid32012	partyid72012	education2012	birthyear2012	statefips2012	news2012    ideology2012)
rename (CC14_321		CC14_325	gender_14	race_14		pid3_14	pid7_14	educ_14	birthyr_14	inputstate_14	newsint_14		ideo5_14)(climatechange2014		environment2014	gender2014	race2014		partyid32014	partyid72014	education2014	birthyear2014	statefips2014	news2014    ideology2014)

gen weight2010=weight
gen weight2012=weight
gen weight2014=weight

keep caseid weight2010 weight2012 weight2014 climatechange2010		environment2010	gender2010	race2010		partyid32010	partyid72010	education2010	birthyear2010	statefips2010	news2010		ideology2010 	climatechange2012		environment2012	gender2012	race2012		partyid32012	partyid72012	education2012	birthyear2012	statefips2012	news2012		ideology2012  climatechange2014		environment2014	gender2014	race2014		partyid32014	partyid72014	education2014	birthyear2014	statefips2014	news2014		ideology2014


reshape long weight climatechange  environment gender race partyid3 partyid7 education birthyear statefips news ideology, i(caseid) j(year_match)


merge m:1 statefips using statefips.dta

drop _merge

rename stateid state

merge m:1 state year_match using allstates_formerge_final

keep if _merge==3

keep if year_match==2014
gen age=year_match-birthyear

gen male=gender
replace male=0 if gender==2
replace male=. if gender==. | gender==8 | gender==9

gen college=0
replace college=1 if educ>=5

gen white=0
replace white=1 if race==1

gen black=0
replace black=1 if race==2

gen hispanic=0
replace hispanic=1 if race==3

gen asian=0
replace asian=1 if race==4

gen other=0
replace other=1 if race>=5

gen ppa=partyid3
replace ppa=. if partyid3>=4


save cces_14frompanel.dta, replace

clear all
use cces_2014.dta
rename (	CC14_326_1	CC14_326_2	CC14_326_3	CC14_326_4	gender	race	CC350	pid3	pid7	educ	birthyr	inputstate newsint ideo5)(	env_a	env_b	env_c	env_d	gender	race	partyreg	partyid3	partyid7	education	birthyear	statefips news ideology)
keep 	env_a	env_b	env_c	env_d	gender	race	partyreg	partyid3	partyid7	education	birthyear	statefips news weight ideology
gen year_match=2014
gen age=year_match-birthyear
save cces_14_formerge, replace

clear all
use cces_2015.dta
rename (	CC15_323_1	CC15_323_2	CC15_323_3	CC15_323_4	gender	race	CC15_350	pid3	pid7	educ	birthyr	inputstate newsint ideo5)(	env_a	env_b	env_c	env_d	gender	race	partyreg	partyid3	partyid7	education	birthyear	statefips news ideology)
keep 	env_a	env_b	env_c	env_d	gender	race	partyreg	partyid3	partyid7	education	birthyear	statefips news weight ideology
gen year_match=2015
gen age=year_match-birthyear
save cces_15_formerge, replace

clear all
use cces_2016.dta
rename (	CC16_333a	CC16_333b	CC16_333c	CC16_333d	gender	race	CC16_360	pid3	pid7	educ	birthyr	inputstate newsint commonweight ideo5)(	env_a	env_b	env_c	env_d	gender	race	partyreg	partyid3	partyid7	education	birthyear	statefips news weight ideology)
keep env_a	env_b	env_c	env_d	gender	race	partyreg	partyid3	partyid7	education	birthyear	statefips news weight ideology
gen year_match=2016
gen age=year_match-birthyear
save cces_16_formerge, replace

***End part I.


***Part II.
***Taking intermediate data files from Part I, defining variables for analysis, merging in gubernatorial election data

clear all
use cces_06_formerge.dta


gen male=gender
replace male=0 if gender==2
replace male=. if gender==. | gender==8 | gender==9

gen college=0
replace college=1 if educ>=5 & educ!=.
replace college=. if educ==.



gen white=0
replace white=1 if race==1

gen black=0
replace black=1 if race==2

gen hispanic=0
replace hispanic=1 if race==3

gen asian=0
replace asian=1 if race==4

gen other=0
replace other=1 if race>=5

gen ppa=partyid3
replace ppa=. if partyid3>=4



merge m:1 stateid using statefips
rename stateid state

drop _merge
merge m:1 state year_match using allstates_formerge_with2006

keep if _merge==3

drop _merge

save cces_06toappend.dta, replace

clear all
use cces_07_formerge.dta


gen male=gender
replace male=0 if gender==2
replace male=. if gender==. | gender==8 | gender==9

gen college=0
replace college=1 if educ>=5 & educ!=.
replace college=. if educ==.


gen white=0
replace white=1 if race==1

gen black=0
replace black=1 if race==2

gen hispanic=0
replace hispanic=1 if race==3

gen asian=0
replace asian=1 if race==4

gen other=0
replace other=1 if race>=5

gen ppa=partyid3
replace ppa=. if partyid3>=4



merge m:1 stateid using statefips
rename stateid state

drop _merge
merge m:1 state year_match using allstates_formerge_final

keep if _merge==3

drop _merge

replace climatechange=. if climatechange==8
replace climatechange=. if climatechange==9

save cces_07toappend.dta, replace

clear all
use cces_09_formerge.dta

append using cces_08_formerge


gen male=gender
replace male=0 if gender==2
replace male=. if gender==. | gender==8 | gender==9

gen college=0
replace college=1 if educ>=5 & educ!=.
replace college=. if educ==.

gen white=0
replace white=1 if race==1

gen black=0
replace black=1 if race==2

gen hispanic=0
replace hispanic=1 if race==3

gen asian=0
replace asian=1 if race==4

gen other=0
replace other=1 if race>=5

gen ppa=partyid3
replace ppa=. if partyid3>=4

merge m:1 statefips using statefips

rename stateid state

drop _merge

merge m:1 state year_match using allstates_formerge_final

keep if _merge==3

drop _merge

save cces_08to09.dta, replace

clear all
use cces_12_formerge.dta
append using cces_11_formerge
append using cces_10_formerge

gen male=gender
replace male=0 if gender==2
replace male=. if gender==. | gender==8 | gender==9

gen college=0
replace college=1 if educ>=5

gen white=0
replace white=1 if race==1

gen black=0
replace black=1 if race==2

gen hispanic=0
replace hispanic=1 if race==3

gen asian=0
replace asian=1 if race==4

gen other=0
replace other=1 if race>=5

gen ppa=partyid3
replace ppa=. if partyid3>=4

merge m:1 statefips using statefips

rename stateid state

drop _merge

merge m:1 state year_match using allstates_formerge_final

keep if _merge==3

drop _merge

save cces_10to12.dta, replace

clear all
use cces_16_formerge.dta
append using cces_15_formerge
append using cces_14_formerge

gen male=gender
replace male=0 if gender==2
replace male=. if gender==. | gender==8 | gender==9

gen college=0
replace college=1 if educ>=5

gen white=0
replace white=1 if race==1

gen black=0
replace black=1 if race==2

gen hispanic=0
replace hispanic=1 if race==3

gen asian=0
replace asian=1 if race==4

gen other=0
replace other=1 if race>=5

gen ppa=partyid3
replace ppa=. if partyid3>=4

merge m:1 statefips using statefips

rename stateid state

drop _merge

merge m:1 state year_match using allstates_formerge_final

keep if _merge==3

drop _merge

save cces_14to16.dta, replace

***End Part II.

***Part III.***
****Takes intermediate data files created in Part II and transforms them into final CCES climate change file file used for analysis
clear all
use cces_14to16.dta

append using cces_10to12
append using cces_08to09
append using cces_07toappend
append using cces_06toappend
append using cces_14frompanel


tab year_match, gen(yfe)
tab gender, gen(male)
gen race_condensed=race
replace race_condensed=5 if race>=5 & race!=.

drop white
gen white=1 if race_condensed==1
replace white=0 if race_condensed!=1 & race_condensed!=.

tab race_condensed, gen (rfe)

tab ppa, gen(pfe)

gen ideo_5=ideology
replace ideo_5=. if ideology>=6 & ideology!=.

summarize ideo_5, detail

gen conservative=0 if ideo_5!=.
replace conservative=1 if ideo_5>=4 & ideo_5!=.


gen liberal=0 if ideo_5!=.
replace liberal=1 if ideo_5<=2 & ideo_5!=.

gen news_informed=0
replace news_informed=1 if news==1 | news==2
replace news_informed=. if news>=8

gen older=0 if age!=.
replace older=1 if age>=45


gen democrat=0
replace democrat=1 if ppa==1
replace democrat=. if ppa==.

gen republican=0
replace republican=1 if ppa==2
replace republican=. if ppa==.


gen independent=0
replace independent=1 if ppa==3
replace independent=. if ppa==.

gen gw_belief=climatechange
replace gw_belief=1 if climatechange==2
replace gw_belief=0 if climatechange>=3 & climatechange!=.


gen r_win=0
replace r_win=1 if m_percent>0
gen int_rd=r_win*m_percent

gen m_percent2=m_percent^2
gen int_rd2=int_rd^2

gen p2Xm=pfe2*m_percent
gen p3Xm=pfe3*m_percent
gen p2Xint=pfe2*int_rd
gen p3Xint=pfe3*int_rd
gen p2Xwin=pfe2*r_win
gen p3Xwin=pfe3*r_win

gen p2Xm2=pfe2*m_percent2
gen p3Xm2=pfe3*m_percent2
gen p2Xint2=pfe2*int_rd2
gen p3Xint2=pfe3*int_rd2

tab statefips, gen(state_fe)

keep if white!=. & gender!=. & republican!=.

save cces_climatechange_foranalysis, replace

**Data ready for analysis**

***Part IV.
****Takes intermediate data files created in Part II and transforms them into the final full CCES sample file used for analysis
clear all
use cces_14to16.dta

append using cces_10to12
append using cces_08to09
append using cces_07toappend
append using cces_06toappend


tab year_match, gen(yfe)
tab gender, gen(male)
gen race_condensed=race
replace race_condensed=5 if race>=5 & race!=.

drop white
gen white=1 if race_condensed==1
replace white=0 if race_condensed!=1 & race_condensed!=.

tab race_condensed, gen (rfe)

tab ppa, gen(pfe)

gen ideo_5=ideology
replace ideo_5=. if ideology>=6 & ideology!=.

summarize ideo_5, detail

gen conservative=0 if ideo_5!=.
replace conservative=1 if ideo_5>=4 & ideo_5!=.


gen liberal=0 if ideo_5!=.
replace liberal=1 if ideo_5<=2 & ideo_5!=.

gen news_informed=0
replace news_informed=1 if news==1 | news==2
replace news_informed=. if news>=8

gen older=0 if age!=.
replace older=1 if age>=45


gen democrat=0
replace democrat=1 if ppa==1
replace democrat=. if ppa==.

gen republican=0
replace republican=1 if ppa==2
replace republican=. if ppa==.


gen independent=0
replace independent=1 if ppa==3
replace independent=. if ppa==.

gen gw_belief=climatechange
replace gw_belief=1 if climatechange==2
replace gw_belief=0 if climatechange>=3 & climatechange!=.


gen r_win=0
replace r_win=1 if m_percent>0
gen int_rd=r_win*m_percent

gen m_percent2=m_percent^2
gen int_rd2=int_rd^2

gen p2Xm=pfe2*m_percent
gen p3Xm=pfe3*m_percent
gen p2Xint=pfe2*int_rd
gen p3Xint=pfe3*int_rd
gen p2Xwin=pfe2*r_win
gen p3Xwin=pfe3*r_win

gen p2Xm2=pfe2*m_percent2
gen p3Xm2=pfe3*m_percent2
gen p2Xint2=pfe2*int_rd2
gen p3Xint2=pfe3*int_rd2

tab statefips, gen(state_fe)

keep if white!=. & gender!=. & republican!=.

save cces_full_foranalysis, replace

**Data ready for analysis**

